package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.UserService;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/*
用户
* */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    //查询用户
    //1.查找用户的基本信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null) {
            return null;
        }
        //2.查找用户的角色,权限
        Integer userId = user.getId();
        //通过用户id去查询角色
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {

            for (Role role : roles) {
                //通过角色id去查询权限
                Integer id = role.getId();
                Set<Permission> permission = permissionDao.findByRoleId(id);
                if (permission != null && permission.size() > 0) {
                    //角色关联权限
                    role.setPermissions(permission);
                }
            }
        }
        //用户关联角色
            user.setRoles(roles);
            return user;
        }

}